package edu.bjfu.klotski;

import java.io.InputStream;

import org.apache.http.util.EncodingUtils;

import edu.bjfu.klotski.UI.Chessboard;
import edu.bjfu.klotski.UI.GameView;
import edu.bjfu.klotski.UI.LayoutFactory;
import edu.bjfu.klotski.UI.ResultHandler;
import edu.bjfu.klotski.core.Interfaces.ILayoutFactory;
import edu.bjfu.klotski.core.Layout.Layout;
import edu.bjfu.klotski.core.Mediator.Mediator;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.support.v4.app.NavUtils;

public class ChessMainActivity extends Activity {

	private ILayoutFactory layoutFactory;
	private Mediator mediator;
	private ResultHandler resultHandler = new ResultHandler();
	private Layout layout;
	private GameView gm;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//取消标题栏
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
                WindowManager.LayoutParams. FLAG_FULLSCREEN);//全屏      
        
        //获取屏幕大小
        DisplayMetrics dm = new DisplayMetrics();   
        getWindowManager().getDefaultDisplay().getMetrics(dm);  
        
        setContentView(R.layout.activity_chess_main);

        //设置棋盘
        gm=(GameView)findViewById(R.id.cm_GameView);
        float rate=(float)dm.widthPixels/(float)ConstCtrl.frame_width;
        int inner_width=(int) Math.ceil(rate*ConstCtrl.frame_inner_width);
        int height=inner_width*5/4+(int) Math.ceil(rate*(ConstCtrl.frame_height-ConstCtrl.frame_innner_height));
	     
        gm.SetWidth(dm.widthPixels);
        gm.SetHeight(height);
        LayoutParams params = gm.getLayoutParams();
        AbsoluteLayout.LayoutParams new_params = new AbsoluteLayout.LayoutParams(params.width, params.height,0, 100);
        gm.setLayoutParams(new_params);
        gm.SetCacheCanvas();
        //gm.SetBackground(R.drawable.frame);
        
        LoadLayout();
    }

    
    private void LoadLayout()
    {
    	try
		{
    		layoutFactory = new LayoutFactory();
    		Intent intent=getIntent();
			String value1=intent.getStringExtra("layoutname");
			InputStream in = getResources().getAssets().open(value1+".xml"); 
			int length = in.available();   
			byte [] buffer = new byte[length];  
			in.read(buffer);  
			String xml = EncodingUtils.getString(buffer, "UTF-8");     
			layout=layoutFactory.Create(xml);
			layout.SimpleCheckAvailableSteps();
			gm.LoadLayout(layout);
		}
		catch(Exception e)
		{
			Log.e("ERR",e.getMessage()); 
		}
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_chess_main, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
//        switch (item.getItemId()) {
//            case android.R.id.home:
//                NavUtils.navigateUpFromSameTask(this);
//                return true;
//        }
        return super.onOptionsItemSelected(item);
    }

}
